package com.yoomiito.app.act;


import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;

import java.util.List;

public class NewFreeBuyAdapter extends BaseMultiItemQuickAdapter<FreeGoodsMultiple, BaseViewHolder> {

    public NewFreeBuyAdapter(@Nullable List<FreeGoodsMultiple> data) {
        super(data);
        addItemType(FreeGoodsMultiple.FREE_GOODS_FLAG, R.layout.item_goods_4);
        addItemType(FreeGoodsMultiple.PERSONAL_GOODS_FLAG,R.layout.item_goods_5);
        addItemType(FreeGoodsMultiple.PERSONAL_GOODS_TITLE_FLAG,R.layout.new_free_buy_personal_title);
    }

    @Override
    protected void convert(BaseViewHolder helper, FreeGoodsMultiple item) {
        if (helper.getItemViewType()==FreeGoodsMultiple.FREE_GOODS_FLAG){//免费商品
            if (helper.getLayoutPosition()==1){
                ImageView iv = helper.getView(R.id.bg);
                iv.setImageResource(R.drawable.bg_free_buy_2);
            }
            CardView cardView = helper.getView(R.id.item_goods_4_view);
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) cardView.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(16);
            lp.rightMargin = DensityUtil.dp2px(16);
            ImageView iv = helper.getView(R.id.goods_iv);
            ImageLoader.getInstance().into(iv.getContext(), item.getFreeGoodsInfo().getImageUrl(), iv);
            ImageView goodsSourceLogoIv = helper.getView(R.id.goods_source_logo);
            goodsSourceLogoIv.setImageResource(item.getFreeGoodsInfo().getSource().equals("1") ? R.mipmap.tmall : R.mipmap.tb);
            helper.setText(R.id.goods_name, item.getFreeGoodsInfo().getTitle());
        }else if(helper.getItemViewType()==FreeGoodsMultiple.PERSONAL_GOODS_FLAG){//专享商品
            ConstraintLayout v = helper.getView(R.id.item_goods_5_view);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) v.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(12);
            lp.rightMargin = DensityUtil.dp2px(12);
            v.setPadding(DensityUtil.dp2px(15),0,DensityUtil.dp2px(15),DensityUtil.dp2px(16));
            if (helper.getLayoutPosition()==getData().size()){
                v.setBackgroundResource(R.drawable.bg_color_bf9880_bottom_l_r_24);
            }else {
                v.setBackgroundResource(R.drawable.bg_color_bf9880_l_r);
            }
            helper.addOnClickListener(R.id.now_buy);
            ImageView goodsPic = helper.getView(R.id.goods_pic);
            ImageLoader.getInstance().into(goodsPic.getContext(), item.getPersonalGoodsInfo().getImageUrl(), goodsPic);
            helper.setText(R.id.goods_name, SpannableUtils.getIconAndText(item.getPersonalGoodsInfo().getTitle(), ResUtil.getDrawable(item.getPersonalGoodsInfo().getSource().equals("1")?R.mipmap.tmall:R.mipmap.tb)));
            helper.setText(R.id.old_goods_price, "原价¥" + SpannableUtils.handlePrice(item.getPersonalGoodsInfo().getZkFinalPrice()));
            helper.setText(R.id.now_goods_price, SpannableUtils.getSmallMoneyFlag("¥" + SpannableUtils.handlePrice(item.getPersonalGoodsInfo().getDiscountPrice())));
        }
    }
}
